//package com.i.template.aspect;
//
//import cn.hutool.json.JSONUtil;
//import com.i.template.domain.ErrorLogDO;
//import com.i.template.service.IUserService;
//import com.i.template.util.HttpUtils;
//import com.i.template.util.UserHolder;
//import jakarta.servlet.http.HttpServletRequest;
//import lombok.RequiredArgsConstructor;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.core.Ordered;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//
//import java.lang.reflect.Method;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Objects;
//
///**
// * @author lewis
// * @type class
// * @description
// * @since 2024/8/7
// */
//@Aspect
//@Component
//@RequiredArgsConstructor
//public class LogAspect implements Ordered {
//    private final IUserService userService;
//    @Pointcut("execution(* com.i.template.controller.*.*(..))")
//    public void exceptionLogPointCut() {}
//
//    @AfterThrowing(value = "exceptionLogPointCut()",throwing = "exception")
//    public void afterThrowingRecord(JoinPoint joinPoint,Throwable exception) {
//        // 获取RequestAttributes
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//
//        // 从获取RequestAttributes中获取HttpServletRequest的信息
//        HttpServletRequest request = (HttpServletRequest) Objects.requireNonNull(requestAttributes).resolveReference(RequestAttributes.REFERENCE_REQUEST);
//
//        try {
//            // 从切面织入点处通过反射机制获取织入点处的方法
//            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//
//            // 获取切入点所在的方法
//            Method method = signature.getMethod();
//
//            // 获取请求的类名
//            String className = joinPoint.getTarget().getClass().getName();
//
//                ErrorLogDO.builder()
//                        .reqParam(JSONUtil.toJsonStr(converMap(request != null ? request.getParameterMap() : null))) // 请求参数
//                        .method(className + "." + method.getName()) // 请求方法名
//                        .name(exception.getClass().getName()) // 异常名称
//                        .message(stackTraceToString(exception.getClass().getName(), exception.getMessage(), exception.getStackTrace())) // 异常信息
//                        .userId(UserHolder.getUser()) // 操作员ID
//                        .userEmail(userService.getById(UserHolder.getUser()).getEmail())
//                        .uri(request != null ? request.getRequestURI() : null) // 操作URI
//                        .ip(HttpUtils.getIpAddr(Objects.requireNonNull(request))) // 操作员IP
//                        .build();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//    public Map<String, String> converMap(Map<String, String[]> paramMap) {
//        if (paramMap == null) {
//            return null;
//        }
//        Map<String, String> rtnMap = new HashMap<>();
//        for (String key : paramMap.keySet()) {
//            rtnMap.put(key, paramMap.get(key)[0]);
//        }
//        return rtnMap;
//    }
//    public String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {
//        StringBuilder strbuff = new StringBuilder();
//        for (StackTraceElement stet : elements) {
//            strbuff.append(stet).append("<br/>");
//        }
//        return exceptionName + ":" + exceptionMessage + "<br/>" + strbuff.toString();
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}
